package config

import (
	"os"
	"path/filepath"
)

// Config 存储应用程序配置
type Config struct {
	ServerAddress string
	DBPath        string
}

// LoadConfig 加载应用程序配置
func LoadConfig() *Config {
	// 默认配置
	cfg := &Config{
		ServerAddress: ":8080",
		DBPath:        "./knowledge.db",
	}

	// 从环境变量加载配置（如果存在）
	if port := os.Getenv("SERVER_PORT"); port != "" {
		cfg.ServerAddress = ":" + port
	}

	if dbPath := os.Getenv("DB_PATH"); dbPath != "" {
		cfg.DBPath = dbPath
	} else {
		// 确保数据库文件路径是绝对路径
		absPath, err := filepath.Abs(cfg.DBPath)
		if err == nil {
			cfg.DBPath = absPath
		}
	}

	return cfg
}
